package com.ruoyi.report_lqn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqn.domain.bo.ReportLqnLqcjlnfxjlbBaDetailBo;
import com.ruoyi.report_lqn.domain.vo.ReportLqnLqcjlnfxjlbBaDetailVo;
import com.ruoyi.report_lqn.domain.ReportLqnLqcjlnfxjlbBaDetail;
import com.ruoyi.report_lqn.mapper.ReportLqnLqcjlnfxjlbBaDetailMapper;
import com.ruoyi.report_lqn.service.IReportLqnLqcjlnfxjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青与粗集料的黏附性试验检测记录-详情Service业务层处理
 *
 * @author tll
 * @date 2024-09-12
 */
@RequiredArgsConstructor
@Service
public class ReportLqnLqcjlnfxjlbBaDetailServiceImpl implements IReportLqnLqcjlnfxjlbBaDetailService {

    private final ReportLqnLqcjlnfxjlbBaDetailMapper baseMapper;

    /**
     * 查询沥青与粗集料的黏附性试验检测记录-详情
     */
    @Override
    public ReportLqnLqcjlnfxjlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询沥青与粗集料的黏附性试验检测记录-详情列表
     */
    @Override
    public TableDataInfo<ReportLqnLqcjlnfxjlbBaDetailVo> queryPageList(ReportLqnLqcjlnfxjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqnLqcjlnfxjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportLqnLqcjlnfxjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青与粗集料的黏附性试验检测记录-详情列表
     */
    @Override
    public List<ReportLqnLqcjlnfxjlbBaDetailVo> queryList(ReportLqnLqcjlnfxjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportLqnLqcjlnfxjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqnLqcjlnfxjlbBaDetail> buildQueryWrapper(ReportLqnLqcjlnfxjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqnLqcjlnfxjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportLqnLqcjlnfxjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getBianh()), ReportLqnLqcjlnfxjlbBaDetail::getBianh, bo.getBianh());
        lqw.eq(StringUtils.isNotBlank(bo.getJillj()), ReportLqnLqcjlnfxjlbBaDetail::getJillj, bo.getJillj());
        lqw.eq(bo.getJilwd() != null, ReportLqnLqcjlnfxjlbBaDetail::getJilwd, bo.getJilwd());
        lqw.eq(bo.getLiqwd() != null, ReportLqnLqcjlnfxjlbBaDetail::getLiqwd, bo.getLiqwd());
        lqw.eq(bo.getJilsj() != null, ReportLqnLqcjlnfxjlbBaDetail::getJilsj, bo.getJilsj());
        lqw.eq(bo.getLengqsj() != null, ReportLqnLqcjlnfxjlbBaDetail::getLengqsj, bo.getLengqsj());
        lqw.eq(bo.getJinzsj() != null, ReportLqnLqcjlnfxjlbBaDetail::getJinzsj, bo.getJinzsj());
        lqw.eq(StringUtils.isNotBlank(bo.getBoljg1()), ReportLqnLqcjlnfxjlbBaDetail::getBoljg1, bo.getBoljg1());
        lqw.eq(StringUtils.isNotBlank(bo.getBoljg2()), ReportLqnLqcjlnfxjlbBaDetail::getBoljg2, bo.getBoljg2());
        lqw.eq(bo.getDengjPjz() != null, ReportLqnLqcjlnfxjlbBaDetail::getDengjPjz, bo.getDengjPjz());
        lqw.eq(StringUtils.isNotBlank(bo.getZonghpd()), ReportLqnLqcjlnfxjlbBaDetail::getZonghpd, bo.getZonghpd());
        return lqw;
    }

    /**
     * 新增沥青与粗集料的黏附性试验检测记录-详情
     */
    @Override
    public Boolean insertByBo(ReportLqnLqcjlnfxjlbBaDetailBo bo) {
        ReportLqnLqcjlnfxjlbBaDetail add = BeanUtil.toBean(bo, ReportLqnLqcjlnfxjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改沥青与粗集料的黏附性试验检测记录-详情
     */
    @Override
    public Boolean updateByBo(ReportLqnLqcjlnfxjlbBaDetailBo bo) {
        ReportLqnLqcjlnfxjlbBaDetail update = BeanUtil.toBean(bo, ReportLqnLqcjlnfxjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqnLqcjlnfxjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青与粗集料的黏附性试验检测记录-详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
